package com.zmm.weblog.admin.event.subscriber;

import com.zmm.weblog.admin.event.ReadArticleEvent;
import com.zmm.weblog.common.domain.mapper.ArticleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author: 福禄寿
 * @Date: 2024/8/22 下午3:53
 * @Des: weblog-springboot
 **/
@Component
@Slf4j
public class ReadArticleSubscriber implements ApplicationListener<ReadArticleEvent> {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(ReadArticleEvent event) {
        Long articleId = event.getArticleId();

        String threadName = Thread.currentThread().getName();
        log.info("线程名称: {}, 事件: {}", threadName, event);
        log.info("文章阅读事件消费成功,articleId:{}", articleId);
        articleMapper.increaseReadNum(articleId);
    }
}
